uniform int numTextures;
uniform sampler2D textures[3];
uniform float blendFactors[3];
uniform int blendModes[3];
uniform vec4 materialColor;

void main(){
    vec4 color = materialColor;
    
    for (int i = 0; i < 3 && i < numTextures; i++) {
        vec4 texColor = texture2D(textures[i], gl_TexCoord[i]);
        if (blendModes[i] == 0) {
            color = mix(color, texColor, blendFactors[i]);
        }
        if (blendModes[i] == 1) {
            if (blendFactors[i] == 1.0) {
                color = color * texColor;
            } else {
                vec4 tmpColor = mix(color, texColor, 1.0);
                color = mix(color, tmpColor, blendFactors[i]);
            }
        }
    }
    
    gl_FragColor = color;
}